// campus_whole_manager.cpp
#include "campus_whole_manager.h"
#include "../log/campus_logger.h"

CampusWholeManager::CampusWholeManager() {
    // 1. 初始化服务器（单例）
    whole_server_ = CampusWholeServer::getInstance();
    
    // 2. 初始化SQL连接（自身作为SQL观察者，转发结果给LoginManager）
    sql_connector_ = CampusSqlConnector::getInstance();
    sql_connector_->setObserver(this);

    initLoginManager();
    initPersonInfoManager();
    initClubInfoManager();
    initLiveManager();
}
void CampusWholeManager::handleClubCreate(websocketpp::connection_hdl hdl, const json JMsg)
{
    club_info_manager_->handleClubCreate(hdl, JMsg);
}

void CampusWholeManager::handleClubSearch(websocketpp::connection_hdl hdl, const json JMsg)
{
    club_info_manager_->handleClubSearch(hdl, JMsg);
}

void CampusWholeManager::handleClubJoin(websocketpp::connection_hdl hdl, const json JMsg)
{
    club_info_manager_->handleClubJoin(hdl, JMsg);
}

void CampusWholeManager::handleClubGroup(websocketpp::connection_hdl hdl, const json JMsg)
{
    club_info_manager_->handleClubGroup(hdl, JMsg);
}

void CampusWholeManager::handleClubInfo(websocketpp::connection_hdl hdl, const json JMsg)
{
    club_info_manager_->handleClubInfo(hdl,JMsg);
}

void CampusWholeManager::handleGroupChat(websocketpp::connection_hdl hdl, const json JMsg)
{
    club_info_manager_->handleGroupChat(hdl,JMsg);
}

void CampusWholeManager::initLoginManager()
{
    login_manager_ = std::make_shared<CampusLoginManager>(whole_server_, sql_connector_);
    whole_server_->setLoginObserver(this);
}

void CampusWholeManager::initPersonInfoManager()
{
    person_info_manager_ = std::make_shared<CampusPersonInfoManager>(whole_server_, sql_connector_);
    whole_server_->setPersonInfoObserver(this);
}

void CampusWholeManager::initClubInfoManager()
{
    club_info_manager_ = std::make_shared<CampusClubInfoManager>(whole_server_, sql_connector_);
    whole_server_->setClubInfoObserver(this);
}

void CampusWholeManager::initLiveManager()
{
    live_info_manager_ = std::make_shared<CampusLiveInfoManager>(whole_server_);
    whole_server_->setLiveObserver(this);
}


// 启动所有资源
void CampusWholeManager::start() {
    // 1. 启动服务器
    if (whole_server_->init() != 0) {
        CampusLogger::warn("CampusWholeManager: whole_server init fail");
        return;
    }
    if (whole_server_->run() != 0) {
        CampusLogger::warn("CampusWholeManager: whole_server run fail");
        return;
    }

    // 2. 启动SQL连接
    if (sql_connector_->init() != 0) {
        CampusLogger::warn("CampusWholeManager: sql_connector init fail");
        return;
    }
    if (sql_connector_->run() != 0) {
        CampusLogger::warn("CampusWholeManager: sql_connector run fail");
        return;
    }

    CampusLogger::info("CampusWholeManager: start success");
}

// 等待资源线程退出
void CampusWholeManager::wait() {
    // 1. 等待SQL线程
    if (sql_connector_->wait() != 0) {
        CampusLogger::warn("CampusWholeManager: sql_connector wait fail");
        return;
    }
    // 2. 等待服务器线程
    if (whole_server_->wait() != 0) {
        CampusLogger::warn("CampusWholeManager: whole_server wait fail");
        return;
    }

    CampusLogger::info("CampusWholeManager: wait success");
}

// -------------------------- CampusLoginServerObserver 接口实现（转发）--------------------------
void CampusWholeManager::handleLoginSalt(websocketpp::connection_hdl hdl, json JMsg) {
    login_manager_->handleLoginSalt(hdl, JMsg);
}

void CampusWholeManager::handleLogin(websocketpp::connection_hdl hdl, json JMsg) {
    login_manager_->handleLogin(hdl, JMsg);
}

void CampusWholeManager::handleRegister(websocketpp::connection_hdl hdl, const json JMsg) {
    login_manager_->handleRegister(hdl, JMsg);
}

void CampusWholeManager::handleLogout(websocketpp::connection_hdl hdl, json JMsg) {
    login_manager_->handleLogout(hdl, JMsg);
}

void CampusWholeManager::handleHdlClose(websocketpp::connection_hdl hdl) {
    login_manager_->handleHdlClose(hdl);
}

// -------------------------- CampusPersonInfoServerObserver 接口实现（转发）--------------------------
void CampusWholeManager::handlePersonInfo(websocketpp::connection_hdl hdl, const json JMsg)
{
    person_info_manager_->handlePersonInfo(hdl, JMsg);
}

void CampusWholeManager::handlePersonInfoModify(websocketpp::connection_hdl hdl, const json JMsg)
{
    person_info_manager_->handlePersonInfoModify(hdl, JMsg);
}


// -------------------------- CampusSqlConnectorObserver 接口实现（转发）--------------------------
void CampusWholeManager::onSelectSucess(std::vector<std::vector<std::pair<std::string, std::string>>>& result, CampusSqlInfo* userData) {
    if(dynamic_cast<CampusLoginInfo*>(userData))
    {
        login_manager_->onSelectSucess(result, userData);
    }
    else if(dynamic_cast<CampusPersonInfo*>(userData))
    {
        person_info_manager_->onSelectSucess(result, userData);
    }
    else if(dynamic_cast<CampusClubInfo*>(userData)){
        club_info_manager_->onSelectSucess(result, userData);
    }
    
}

void CampusWholeManager::onSelectFail(CampusSqlInfo* userData) {
    if(dynamic_cast<CampusLoginInfo*>(userData))
    {
        login_manager_->onSelectFail(userData);
    }
    else if(dynamic_cast<CampusPersonInfo*>(userData))
    {
        person_info_manager_->onSelectFail(userData);
    }
    else if(dynamic_cast<CampusClubInfo*>(userData)){
        club_info_manager_->onSelectFail(userData);
    }
}

void CampusWholeManager::onUpdateSucess(CampusSqlInfo* userData) 
{
    if(dynamic_cast<CampusLoginInfo*>(userData))
    {
        login_manager_->onUpdateSucess(userData);
    }
    else if(dynamic_cast<CampusPersonInfo*>(userData))
    {
        person_info_manager_->onUpdateSucess(userData);
    }
    else if(dynamic_cast<CampusClubInfo*>(userData)){
        club_info_manager_->onUpdateSucess(userData);
    }
}

void CampusWholeManager::onUpdateFail(CampusSqlInfo* userData)
{
    if(dynamic_cast<CampusLoginInfo*>(userData))
    {
        login_manager_->onUpdateFail(userData);
    }
    else if(dynamic_cast<CampusPersonInfo*>(userData))
    {
        person_info_manager_->onUpdateFail(userData);
    }
    else if(dynamic_cast<CampusClubInfo*>(userData)){
        club_info_manager_->onUpdateFail(userData);
    }
}

void CampusWholeManager::onInsertSucess(CampusSqlInfo* userData) {
    if(dynamic_cast<CampusLoginInfo*>(userData))
    {
        login_manager_->onInsertSucess(userData);
    }
    else if(dynamic_cast<CampusPersonInfo*>(userData))
    {
        person_info_manager_->onInsertSucess(userData);
    }
    else if(dynamic_cast<CampusClubInfo*>(userData)){
        club_info_manager_->onInsertSucess(userData);
    }
}

void CampusWholeManager::onInsertFail(CampusSqlInfo* userData) {
    if(dynamic_cast<CampusLoginInfo*>(userData))
    {
        login_manager_->onInsertFail(userData);
    }
    else if(dynamic_cast<CampusPersonInfo*>(userData))
    {
        person_info_manager_->onInsertFail(userData);
    }
    else if(dynamic_cast<CampusClubInfo*>(userData)){
        club_info_manager_->onInsertFail(userData);
    }   
}

void CampusWholeManager::handleLiveJoin(websocketpp::connection_hdl hdl, const json JMsg)
{
    live_info_manager_->handleLiveJoin(hdl, JMsg);
}

void CampusWholeManager::handleLiveLeave(websocketpp::connection_hdl hdl)
{
    live_info_manager_->handleLiveLeave(hdl);
}

void CampusWholeManager::handleLiveChat(websocketpp::connection_hdl hdl, const json JMsg)
{
    live_info_manager_->handleLiveChat(hdl, JMsg);
}